package com.mszlu.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.blog.dao.dos.Archives;
import com.mszlu.blog.dao.mapper.ArticleBodyMapper;
import com.mszlu.blog.dao.mapper.ArticleMapper;
import com.mszlu.blog.dao.pojo.Article;
import com.mszlu.blog.dao.pojo.ArticleBody;
import com.mszlu.blog.service.ArticleService;
import com.mszlu.blog.service.CategoryService;
import com.mszlu.blog.service.SysUserService;
import com.mszlu.blog.service.TagService;
import com.mszlu.blog.vo.ArticleBodyVo;
import com.mszlu.blog.vo.ArticleVo;
import com.mszlu.blog.vo.Result;
import com.mszlu.blog.vo.params.PageParams;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.Record;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
   private ArticleMapper articleMapper;
    @Autowired
    private TagService tagService;
    @Autowired
    private SysUserService sysUserService;
    @Override
    public Result listArticle(PageParams pageParams) {
        //int i = 10/0;
        /**
         * 分页查询 article 数据库表
         */
        Page<Article> page = new Page<>(pageParams.getPage(), pageParams.getPageSize());
        //排序方式
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //按时间倒序
        //是否置顶排序
        queryWrapper.orderByDesc(Article::getWeight,Article::getCreateDate);
        Page<Article> articlePage = articleMapper.selectPage(page,queryWrapper);
        List<Article> records = articlePage.getRecords();
        //不能直接返回
        List<ArticleVo> articleVoList = copyList(records,true,true);
        return Result.success(articleVoList);
    }

    @Override
    public Result hotArticle(int limit) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //排序方式
        queryWrapper.orderByDesc(Article::getViewCounts);
        //排序后需要的 id title
        queryWrapper.select(Article::getId,Article::getTitle);
        // 条数
        queryWrapper.last("limit " + limit);
        List<Article> articles = articleMapper.selectList(queryWrapper);
        return Result.success(copyList(articles,false,false));
        //select id,title from article order by view_count desc limit 5
    }

    //最新文章
    @Override
    public Result newArticle(int limit) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //排序方式 时间
        queryWrapper.orderByDesc(Article::getCreateDate);
        //排序后需要的 id title
        queryWrapper.select(Article::getId,Article::getTitle);
        // 条数
        queryWrapper.last("limit " + limit);
        List<Article> articles = articleMapper.selectList(queryWrapper);
        return Result.success(copyList(articles,false,false));
        //select id,title from article order by create_date desc limit 5
    }

    @Override
    public Result listArchives() {
        List<Archives> archivesList = articleMapper.listArchives();
        return Result.success(archivesList);
    }

    @Override
    public Result findArticleById(Long articleId) {
        /**
         * 1.根据id查询文章信息
         * 2.根据bodyId和categoryId 去关联查询
         */
        Article article = this.articleMapper.selectById(articleId);
        ArticleVo articleVo = copy(article, true, true,true,true);

        return Result.success(articleVo);
    }

    @Autowired
    private CategoryService categoryService;

    private List<ArticleVo> copyList(List<Article> records,boolean isTag, boolean isAuthor){
            List<ArticleVo> articleVoList = new ArrayList<>();
            for(Article record : records){
                articleVoList.add(copy(record,isTag,isAuthor,false,false));
            }
            return articleVoList;
        }
    //重载
    private List<ArticleVo> copyList(List<Article> records,boolean isTag, boolean isAuthor, boolean isBody, boolean isCategory){
        List<ArticleVo> articleVoList = new ArrayList<>();
        for(Article record : records){
            articleVoList.add(copy(record,isTag,isAuthor,isBody,isCategory));
        }
        return articleVoList;
    }

        private ArticleVo copy(Article article,boolean isTag, boolean isAuthor, boolean isBody, boolean isCategory){
            ArticleVo articleVo = new ArticleVo();
            BeanUtils.copyProperties(article,articleVo);
            articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
            // 并不是所有的接口 都需要标签， 作者信息的
            if(isTag)
            {
                Long articleId = article.getId();
                articleVo.setTags(tagService.findTagsByArticleId(articleId));
            }
            if(isAuthor){
                Long authorId = article.getAuthorId();
                articleVo.setAuthor(sysUserService.findUserById(authorId).getNickname());
            }
            if(isBody){
                Long bodyId = article.getBodyId();
                articleVo.setBody(findArticleBodyById(bodyId));
            }
            if(isCategory){
                Long categoryId = article.getCategoryId();
                articleVo.setCategory(categoryService.findCategoryById(categoryId));
            }
            return articleVo;
        }

        @Autowired
        private ArticleBodyMapper articleBodyMapper;

        private ArticleBodyVo findArticleBodyById(Long bodyId) {
            ArticleBody articleBody = articleBodyMapper.selectById(bodyId);
            ArticleBodyVo articleBodyVo = new ArticleBodyVo();
            articleBodyVo.setContent(articleBody.getContent());
            return articleBodyVo;
        }


}
